﻿using HealthcareSystem.API.Write.Application.Command.MetricsCommands;
using HealthcareSystem.Domain.BasicSettings.Metrics;
using MediatR;
using NLog.Web.LayoutRenderers;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel.MetricsCommands
{
    public class MetricsUpdateCommandHandler : IRequestHandler<MetricsUpdateCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<Metricss> metssRep;

        public MetricsUpdateCommandHandler(IBaseRepository<Metricss> metssRep)
        {
            this.metssRep = metssRep;
        }

        public async Task<ApiResult<int>> Handle(MetricsUpdateCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result=new ApiResult<int>();
            result.Code=ResultCode.Success;
            result.Message=ResultCode.Success.ToString();
            
            var res=metssRep.GetAll().FirstOrDefault(x=>x.MetricsId==request.MetricsId);
            if (res!= null)
            {
                res.ListofdiseasesId=request.ListofdiseasesId;
                res.MetricsName=request.MetricsName;
                res.MetricsCode=request.MetricsCode;
                res.MetricsClassId=request.MetricsClassId;
                res.IsDeleted = false;
                await metssRep.UpdAsync(res);
                return await Task.FromResult(result);
            }
            else
            {
                result.Code = ResultCode.Error;
                result.Message = ResultCode.Error.ToString();
                return await Task.FromResult(result);
            }


        }
    }
}
